/*
 * @Descripttion: 消息提醒
 * @version: V1.0
 * @Author: GW
 * @Date: 2021-07-29 10:40:42
 * @LastEditors: GW
 * @LastEditTime: 2022-01-19 09:09:51
 */
import { count, list, read } from "@/api/message/msg";

const msg = {
    state: {
        msgNumber: {},  //消息未读数量
        msgList: [],    //消息未读列表
        msgListAll: {},  //列表所有数据
        msgReceive: true,//只调用一次 触发接收消息（因为设置了setInterval（），所有全站只调用一次）
    },
    getters: {

    },
    mutations: {
        MSG_NUMBER (state, newData) {
            state.msgNumber = newData;
        },
        MSG_LIST (state, newData) {
            state.msgList = newData;
        },
        MSG_RECEIVE (state, newData) {
            state.msgReceive = newData;
        },
        MSG_LIST_ALL (state, newData) {
            state.msgListAll = newData;
        }
    },
    actions: {
        actionsGetNumber ({ commit }) {
            //消息数量
            return new Promise((resolve, reject) => {
                //status:0 未读 1 已读
                count({ status: 0 }).then(res => {
                    if (res.code === 200 && res.data != null) {
                        commit('MSG_NUMBER', res.data);
                        // resolve()
                    }
                }).catch(error => {
                    reject(error);
                });
            })
        },
        actionsGetList ({ commit, state }, requestData) {
            //消息列表
            return new Promise((resolve, reject) => {
                let request = {}
                if (!requestData) {
                    request.current = 1;
                    request.size = 10;
                } else {
                    request = Object.assign(request, requestData);
                }
                list(request).then(res => {
                    if (res.code === 200 && res.data.records.length > 0) {
                        console.log(state.msgList.length);
                        //只拿最新十条数据
                        if (state.msgList.length > 0) {
                            let obj = state.msgList.concat(res.data.records);
                            commit('MSG_LIST', obj);
                        } else {
                            commit('MSG_LIST', res.data.records);
                        }
                        commit('MSG_LIST_ALL', res.data);
                        console.log(state.msgList)
                        resolve(true)
                    } else {
                        commit('MSG_LIST', []);
                    }
                }).catch(error => {
                    reject(false);
                });
            })
        },
        actionsGetRead ({ commit }, id) {
            //消息阅读
            return new Promise((resolve, reject) => {
                let request = [id]
                read(request).then(res => {
                    if (res.code === 200) {
                        resolve(true)
                    }
                }).catch(error => {
                    reject(false);
                });
            })
        },
    },

}
export default msg;